<?php

/**
 * gallery actions.
 *
 * @package    videosurv
 * @subpackage gallery
 * @author     Mina Gerges
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class image_processingActions extends sfActions
{

    /**
     * action execute before any action
     */
    public function preExecute()
    {
        $this->user = $this->getUser()->getAttribute('user');
        $this->companyData = $this->getUser()->getAttribute('company');
    }

    /**
     * Executes GetImageUrl action
     *
     * @param sfRequest $request A request object
     */
    public function executeRenderImage(sfWebRequest $request)
    {
        $type = $request->getParameter('type');
        switch ($type)
        {
            case 'cat': //User photo
                $defaultPhoto = sfConfig::get('app_default_category_image');
                $photoDir     = sfConfig::get('app_dir_category_images');
                break;
            case 'us': //User photo
                $defaultPhoto = sfConfig::get('app_default_user_image');
                $photoDir     = sfConfig::get('app_dir_user_images');
                break;
            case 'co': //company photo
                $defaultPhoto = sfConfig::get('app_default_company_image');
                $photoDir     = sfConfig::get('app_dir_company_images');
                break;
            default :
                $this->forward404();
                break;
        }
       $image_path   = sfConfig::get('sf_root_dir') . '/' . $photoDir . '/' . $request->getParameter('imageName','notFound') . '.' . $request->getParameter('format');
        $image        = new MaxImage($image_path);
        $image->default = sfConfig::get('sf_root_dir') . '/' . $photoDir . '/' . $defaultPhoto;
        $width         = $request->getParameter('width');
        $height        = $request->getParameter('height');
        //$this->response->setContent($image->getImage());
        $response      = $this->getResponse();
        $response->clearHttpHeaders();
        $response->setHttpHeader('Content-Transfer-Encoding', ' binary');
        $response->setHttpHeader('Content-Type', 'image/' . $image->getMimeType());
        $response->setHttpHeader('Content-Disposition', 'attachment;filename=' . $image->image);
        $response->setContent($image->resizeImageOnTheFlay($width,$height));
        $this->setLayout(false);
        return sfView::NONE;
    }

}
